package com.example.employeemanager;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.DatabaseHelper;
import ohos.data.dataability.DataAbilityUtils;
import ohos.data.rdb.*;
import ohos.data.resultset.ResultSet;
import ohos.data.dataability.DataAbilityPredicates;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.rpc.MessageParcel;
import ohos.utils.net.Uri;
import ohos.utils.PacMap;

import java.io.*;

public class EmployeeDataAbility extends Ability {

    private static final HiLogLabel LABEL_LOG = new HiLogLabel(3, 0xD001100, "Demo");

    // 关系型数据库操作对象
    private RdbStore mRdbStore;

    // 关系型数据可回调
    private RdbOpenCallback mRdbCallback = new RdbOpenCallback() {
        @Override
        public void onCreate(RdbStore rdbStore) {
            // 在创建test.sqlite数据库时，创建student数据表
            rdbStore.executeSql("CREATE TABLE IF NOT EXISTS employee (" +
                    "id INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    "name TEXT NOT NULL, " +
                    "age INTEGER, " +
                    "sex TINYINT, " +
                    "department TEXT)");
        }

        @Override
        public void onUpgrade(RdbStore rdbStore, int i, int i1) {
            // 更新数据库时回调
        }
    };

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        DatabaseHelper helper = new DatabaseHelper(this);
        StoreConfig config = StoreConfig.newDefaultConfig("test.sqlite");
        mRdbStore = helper.getRdbStore(config, 1, mRdbCallback);
    }

    @Override
    protected void onStop() {
        super.onStop();
        mRdbStore.close();
    }

    @Override
    public ResultSet query(Uri uri, String[] columns, DataAbilityPredicates predicates) {
        // 转换谓词对象
        RdbPredicates rdbPredicates = DataAbilityUtils
                .createRdbPredicates(predicates,"employee");
        // 查询结果
        ResultSet resultSet = mRdbStore.query(rdbPredicates, columns);
        return resultSet;
    }

    @Override
    public int insert(Uri uri, ValuesBucket value) {

        int id = (int) mRdbStore.insert("employee", value);
        return id;
    }

    @Override
    public int delete(Uri uri, DataAbilityPredicates predicates) {
        // 转换谓词对象
        RdbPredicates rdbPredicates = DataAbilityUtils
                .createRdbPredicates(predicates,"employee");
        // 删除员工信息
        int rowNum = mRdbStore.delete(rdbPredicates);
        return rowNum;
    }

    @Override
    public int update(Uri uri, ValuesBucket value, DataAbilityPredicates predicates) {

        // 转换谓词对象
        RdbPredicates rdbPredicates = DataAbilityUtils
                .createRdbPredicates(predicates,"employee");
        // 更新员工信息
        int rowNum = mRdbStore.update(value, rdbPredicates);
        return rowNum;
    }

    @Override
    public FileDescriptor openFile(Uri uri, String mode) {
        // 获取文件名
        String filename = uri.getDecodedPathList().get(1);
        // 文件路径
        String path = getDataDir()
                + File.separator + filename;
        // 文件对象
        File file = new File(path);
        try {
            // 如果文件不存在，那么先创建该文本文件
            // 并且加入文本“测试数据”
            if (!file.exists()) {
                createFile(path);
            }
            // 获取文件描述符对象并返回
            FileInputStream fileIs = new FileInputStream(file);
            FileDescriptor fd = fileIs.getFD();
            return MessageParcel.dupFileDescriptor(fd);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 创建文本文件
     */
    private static void createFile(String path) throws IOException {
        // 获得BufferedWriter对象
        BufferedWriter bw = new BufferedWriter(new FileWriter(path));
        bw.write("测试数据"); // 写字符串
        bw.close(); //关闭BufferedWriter
    }

    @Override
    public String[] getFileTypes(Uri uri, String mimeTypeFilter) {
        return new String[0];
    }

    @Override
    public PacMap call(String method, String arg, PacMap extras) {
        return null;
    }

    @Override
    public String getType(Uri uri) {
        return null;
    }


}